package base.utils

import android.content.Context
import android.database.Cursor
import android.net.Uri
import dalvik.system.DexFile
import android.provider.MediaStore
import java.io.IOException
import java.text.DecimalFormat
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*


/**
 * Created by Jack on 2017/10/12.
 */
object StringUtils {
    val letterList: MutableList<String> = ArrayList()

    init {
        for (i in 0..25) {
            val str = String(Character.toChars(97 + i))
            letterList.add(str.toUpperCase())
        }
    }

//    fun StringFormat(StringId: Int, vararg strings: Any): String {
//        return String.format(ApplicationContext.context.getString(StringId), strings)
//    }

    fun getRealPathFromURI(context: Context, contentUri: Uri): String {
        var cursor: Cursor? = null
        try {
            val proj = arrayOf(MediaStore.Images.Media.DATA)
            cursor = context.getContentResolver().query(contentUri, proj, null, null, null)
            val column_index = cursor!!.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
            cursor!!.moveToFirst()
            return cursor!!.getString(column_index)
        } finally {
            if (cursor != null) {
                cursor!!.close()
            }
        }
    }


    fun StringFormat(mContext: Context, StringId: Int, vararg strings: Any): String {
        return String.format(mContext.getString(StringId), *strings)
    }

    fun isBlank(str: String?): Boolean {
        return str == null || str.trim { it <= ' ' }.isEmpty()
    }

    fun isNotBlank(str: String?): Boolean {
        return !isBlank(str)
    }

    fun isEmpty(str: String?): Boolean {
        return (str == null || str.length == 0) && isBlank(str)
    }

    fun BigDecimalNumber(num: Double): String {
        val decimalFormat = DecimalFormat("#0.00")
        return decimalFormat.format(num)
    }

    fun format2PointNumber(s : Any): String {
        return String.format("%.2f", s)
    }

    fun LongDateToString(time: Long): String {
        if (time == 0L) {
            return ""
        }
        val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        val date = Date(time)
        return format.format(date)
    }

    fun getActivityClass(mContext: Context, packageName: String, className: String?): Class<*>? {
        if (className == null) {
            return null
        }
        var clazz: Class<*>? = null
        try {
            val classWholeName = getClassesOfPackage(mContext, packageName, className)
            if (classWholeName != null) {
                clazz = Class.forName(classWholeName)
            }
        } catch (e: ClassNotFoundException) {
            e.printStackTrace()
        }

        return clazz
    }

    fun getClassesOfPackage(mContext: Context, packageName: String, className: String): String? {
        var classWholeName: String? = null
        try {
            val packageCodePath = mContext.getPackageCodePath()
            val df = DexFile(packageCodePath)
            val iter = df.entries()
            while (iter.hasMoreElements()) {
                val name = iter.nextElement()
                if (name.contains(packageName)) {
                    if (name.contains(className)) {
                        if (name.contains("$")) {
                            classWholeName = name.substring(0, name.lastIndexOf("$"))
                        } else {
                            classWholeName = name
                        }
                        if (classWholeName!!.length == name.lastIndexOf("$")) {
                            break
                        }
                    }
                }
            }
        } catch (e: IOException) {
            e.printStackTrace()
        }

        return classWholeName
    }

    fun getClassesOfPackage(mContext: Context, packageName: String): Array<String> {
        val classes = ArrayList<String>()
        try {
            val packageCodePath = mContext.getPackageCodePath()
            val df = DexFile(packageCodePath)
            val iter = df.entries()
            while (iter.hasMoreElements()) {
                val className = iter.nextElement()
                if (className.contains(packageName)) {
                    if (className.contains("$")) {
                        classes.add(className.substring(0, className.lastIndexOf("$")))
                    } else {
                        classes.add(className)
                    }
                }
            }
        } catch (e: IOException) {
            e.printStackTrace()
        }

        return classes.toArray(arrayOfNulls<String>(classes.size))
    }

    fun parse(strDate: String, pattern: String): Date? {
        try {
            return if (isBlank(strDate)) null else SimpleDateFormat(pattern).parse(strDate)
        } catch (e: ParseException) {
            return Date()
        }

    }

    fun areNotBlank(strings: List<String>): Boolean {
        for (data in strings) {
            if (!isNotBlank(data)) return false
        }
        return true
    }

    fun areBlank(strings: List<String>): Boolean {
        for (data in strings) {
            if (!isBlank(data)) return false
        }
        return true
    }
}